// pages/progressCreate/progressCreate.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pro_id: 0,
    progress: '',
    last_work: '',
    question: '',
    images: [],
    work: '',
    sign:0,
    enterprise:0,
    library:0,
    workTitle:"总体进展情况",
    progressTitle:"本周进展",
    lastWorkTitle:"下周计划",
    type:1
  },

  previewImg2: function (e) {
    let that = this;
    let figureList = that.data.images;
    wx.previewImage({
      current: e.currentTarget.dataset.src,   //当前图片地址
      urls: figureList,        //所有要预览的图片的地址集合 数组形式
    });
  },

  //上传图片
  onChoosePic() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: `${app.globalData.rootUrl}/api/upload/upload`,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: { 'appid': app.appid },
          success: res => {
            let response = JSON.parse(res.data);
            if (res.statusCode == 200) {
              if (response.code == 0) {
                let images = that.data.images;
                images.push(response.thumb);
                that.setData({ images });
              } else {
                app.showFailToast(response.msg);
              }
            } else app.showFailToast('网络错误，请稍后再试');
          }
        });
      }
    })
  },

  //删除图片
  delImage: function (e) {
    var index = e.currentTarget.dataset.v;
    var images = this.data.images;
    images.splice(index,1);
    this.setData({ images: images });
  },

  //提交进展情况
  progress: function () {
    var pid = this.data.pro_id;
    var progress = this.data.progress;
    var work = this.data.work;
    var last_work = this.data.last_work;
    var question = this.data.question;
    var type = this.data.type;

    var params = { pid: pid, type: type, progress: progress, last_work: last_work, question: question, work: work, pic: this.data.images.join(';') };
    app.request('post', '/api/Project/progress', params).then(res => {
      app.showFailToast(res.data.msg);
      if (res.data.code == 0) {
        wx.navigateBack();
      }
    }).catch(err => console.log(err));
  },

  inputArea: function (e) {
    var field = e.currentTarget.dataset.field;
    if (field == "progress") {
      this.setData({ progress: e.detail.value });
    }
    if (field == "last_work") {
      this.setData({ last_work: e.detail.value });
    }
    if (field == "question") {
      this.setData({ question: e.detail.value });
    }
    if (field == "work") {
      this.setData({ work: e.detail.value });
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.proId != undefined) {
      this.setData({ pro_id: options.proId });
    }
    if (options.sign != undefined) {
      this.setData({ sign: options.sign });
      if(options.sign==3){
        this.setData({lastWorkTitle:"下月工作"});
      }
    }

    if (options.enterprise != undefined) {
      this.setData({ enterprise: options.enterprise });
      if(options.enterprise==352){
        this.setData({ workTitle:"进展情况"});
      }else{
        this.setData({ workTitle:"年度累计应税收入"});
      }
      
    }

    if (options.library != undefined) {
      var library = options.library;
      if(library==2 | library==3){
        this.setData({ progressTitle:"本月进展",lastWorkTitle:"下月计划",type:2});
      }
      this.setData({ library: options.library });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})